package com.ly.wxPay.controller;

import com.ly.common.core.bean.R;
import com.ly.wxPay.bean.dto.OrderRefundsDTO;
import com.ly.wxPay.bean.dto.WxOrderDTO;
import com.ly.wxPay.bean.vo.NativeCodeVO;
import com.ly.wxPay.bean.vo.OrderVO;
import com.ly.wxPay.service.IWxService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 支付-核心
 *
 * @author luoyun
 * @Date: 2025-11-22
 */
@Tag(name = "支付-订单表")
@Slf4j
@RestController
@RequestMapping("/wxPay")
@RequiredArgsConstructor
public class WxController {

    private final IWxService wxService;

    /**
     * 统一下单，生成支付二维码
     */
    @Operation(summary = "统一下单")
    @PostMapping("/native")
    public R<NativeCodeVO> nativePay(@Validated @RequestBody WxOrderDTO dto) {
        return wxService.nativePay(dto);
    }

    /**
     * 查询订单-wx
     */
    @Operation(summary = "查询订单-wx")
    @GetMapping("/getOrder/{orderNo}")
    public R<OrderVO> getOrder(@PathVariable String orderNo) {
        return wxService.getOrder(orderNo);
    }

    /**
     * 查询退款-wx
     */
    @Operation(summary = "查询订单-wx")
    @GetMapping("/getRefundOrder/{refundNo}")
    public R<OrderVO> getRefundOrder(@PathVariable String refundNo) {
        return wxService.getRefundOrder(refundNo);
    }

    /**
     * 用户取消订单-wx
     */
    @Operation(summary = "用户取消订单-wx")
    @PostMapping("/cancelOrder/{id}")
    public R cancelOrder(@PathVariable String id) {
        return wxService.cancelOrder(id);
    }

    /**
     * 订单退款-wx
     */
    @Operation(summary = "订单退款-wx")
    @PostMapping("/refundsOrder")
    public R refundOrder(@RequestBody OrderRefundsDTO dto) {
        log.info("申请退款");
        return wxService.refundOrder(dto);
    }

    /**
     * 确定订单
     */
    @Operation(summary = "确定订单-wx")
    @PostMapping("/confirmOrder/{id}")
    public R<String> confirmOrder(@PathVariable Long id) {
        return wxService.confirmOrder(id);
    }

    /**
     * 支付成功回调通知
     *
     * @desc: 微信会在支付成功之后，通知商户，48h内重复通知直到商户返回200
     * 应答超时：微信需要再5s之内返回成功，否则也会重复通知
     */
    @ApiOperation("微信支付通知")
    @PostMapping("/native/notify")
    public String nativeNotify(HttpServletRequest request, HttpServletResponse response) {
        return wxService.nativeNotify(request, response);
    }

    /**
     * 退款成功回调通知
     */
    @ApiOperation("微信退款通知")
    @PostMapping("/refunds/notify")
    public String refundsNotify(HttpServletRequest request, HttpServletResponse response) {
        return wxService.refundsNotify(request, response);
    }

    /**
     * 下载账单-wx
     */
    @Operation(summary = "下载账单-wx")
    @GetMapping("/downloadBill/{billDate}/{type}")
    public R downloadBill(@PathVariable String billDate, @PathVariable String type) {
        return wxService.downloadBill(billDate, type);
    }
}
